package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



@Slf4j
@RestController
public class CommenController {


    /**
     * 文件上传流程： 1.添加依赖在pom.xml
     *              2.修改配置文件 yml,yaml
     *              3.通过引用创建AliOssPeoperties类(自定义的类可直接加加入IOC容器中)
     *              4.创建工具类AliOssUtil，第三方类，需要传入IOC容器中，创建配置类OssConfigration
     *              5.创建上传方法，上传文件
     */
   @Autowired
   private AliOssUtil aliOssUtil;


    @PostMapping("/admin/common/upload")
    public Result<String> upLoad(MultipartFile file) throws Exception {
        try {
            log.info("上传文件名为：{}",file.getOriginalFilename());

            byte [] content= file.getBytes();

            String originalFilename = file.getOriginalFilename();

            String url= aliOssUtil.upload(content,originalFilename);

            log.info("文件上传成功，返回访问路径{}",url);
            return Result.success(url);
        } catch (Exception e) {
            log.error("文件上传失败:{}",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

}
